/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hancadsa.client.view.widget;

import br.com.sales.generics.client.components.mask.MaskEditHandler;
import br.com.sales.generics.client.log.LogBrowser;
import br.com.sales.generics.client.request.HannaCallBack;
import br.com.sales.generics.client.request.HannaRequestCallback;
import br.com.sales.generics.client.utils.NumbersUtils;
import br.com.sales.hancadsa.client.server.HanCadSAService;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.Response;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import java.util.Map;

/**
 *
 * @author v
 */
public class FinishSaleDialog extends PopupPanel {

    private static final NewRequestDialogUiBinder uiBinder = GWT.create(NewRequestDialogUiBinder.class);
    @UiField
    MaskEditHandler totalSaleEdit;
    @UiField
    TextBox discountEdit;
    private Map sale;
    private HannaCallBack callBack;
    @Inject
    private HanCadSAService service;
    private Double value;

    @Inject
    public FinishSaleDialog() {
        super(false, true);
        add(uiBinder.createAndBindUi(this));
    }

    @Override
    protected void onLoad() {

        super.onLoad();
        setWidth("255px");
        setHeight("220px");
        setGlassEnabled(true);
        setModal(true);
        discountEdit.addKeyPressHandler(new NumbersUtils.NumbersOnly());
        discountEdit.addKeyUpHandler(new KeyUpHandler() {

            @Override
            public void onKeyUp(KeyUpEvent event) {
                if (((TextBox) event.getSource()).getValue() != null && !"".equals(((TextBox) event.getSource()).getValue())) {
                    Integer discount = Integer.parseInt(((TextBox) event.getSource()).getValue());
                    totalSaleEdit.setValue("" + (value - (value * (discount / 100D))));
                } else {
                    totalSaleEdit.setValue(value.toString());
                }
            }
        });
        totalSaleEdit.getElement().setId("decimal");
        totalSaleEdit.setEnabled(false);
        discountEdit.setValue("0");
        center();
    }

    @UiHandler("cancelSale")
    public void onClickCancel(ClickEvent event) {
        hide();
    }

    @UiHandler("saveSale")
    public void onClickSave(ClickEvent event) {
        sale.put("DESC", discountEdit.getText());
        sale.put("TOTAL", value);

        service.finishedSales(sale, new HannaRequestCallback() {

            @Override
            public void onResponseReceived(Request request, Response response) {
                LogBrowser.writeConsole("Inseir sales " + response.getText(), null);
                callBack.onReady(response.getText());
                hide();
            }
        });
    }

    private void adjustFields() {
        totalSaleEdit.setValue(value.toString());
    }

    public void show(Map sale, HannaCallBack callBack) {
        this.sale = sale;
        value = NumbersUtils.toNumber(sale.get("TOTAL").toString().replaceFirst("\\.", "").replace(",", ".")).doubleValue();
        this.callBack = callBack;
        adjustFields();
        show();
    }

    interface NewRequestDialogUiBinder extends UiBinder<Widget, FinishSaleDialog> {
    }
}
